package com.example.feifei.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.feifei.net.ApiService;
import com.example.feifei.net.CallBackSubscriber;
import com.example.feifei.net.MySubscriber;
import com.example.feifei.net.RetrofitClient;
import com.example.feifei.newproject.R;
import com.example.feifei.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import javax.security.auth.Subject;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by feifei on 2018/6/5.
 */

public class RXJavaFragment extends Fragment implements View.OnClickListener{

    private View root_view;
    @BindView(R.id.get_rx)
    Button get_rx;
    @BindView(R.id.txt_result)
    TextView txt_result;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        root_view = inflater.inflate(R.layout.fragment_rxjava,null);
        ButterKnife.bind(this,root_view);
        initView();
        get_rx.setOnClickListener(this);
        return root_view;
    }
    /**
     * 初始化视图
     */
    private void initView(){
        get_rx.setText("RXJava网络请求");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.get_rx:
//                httpText();
                myRxNetWork();
                break;
        }
    }

    /**
     * Rx封装请求
     */
    private void myRxNetWork(){
        MySubscriber loader = new MySubscriber();
        loader.getRxNetString().subscribe(new CallBackSubscriber<String>(){
            @Override
            public void onNext(String s) {
                txt_result.setText(s);
                ToastUtil.showMessage(getActivity(),"请求成功");
            }
        });
    }
    //RXjava请求数据
    private void httpText(){
        ApiService service = RetrofitClient.getInstance().getApiService();
        Map<String,String> params = new HashMap<>();
        Observable<String> observable = service.queryUrl(params);
        observable.subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        ToastUtil.showMessage(getActivity(),"请求完成");
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showMessage(getActivity(),"请求失败");
                    }

                    @Override
                    public void onNext(String s) {
                        txt_result.setText(s);
                        ToastUtil.showMessage(getActivity(),"请求成功");
                    }
                });
    }
}
